import { NextRequest, NextResponse } from 'next/server'

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const projectId = (await params).id
    
    // 模拟任务数据
    const tasks = [
      {
        _id: 'task-001',
        title: '完成电机控制模块设计',
        description: '设计并实现电机控制的核心模块，包括PWM控制和保护功能',
        status: 'in_progress' as const,
        priority: 'high' as const,
        assignee: 'user-001',
        assigneeName: '张工程师',
        dueDate: '2024-03-15T00:00:00Z',
        createdAt: '2024-01-10T09:00:00Z',
        updatedAt: '2024-01-20T14:30:00Z',
        projectId
      },
      {
        _id: 'task-002',
        title: '编写技术文档',
        description: '编写项目的技术文档，包括架构设计和接口说明',
        status: 'pending' as const,
        priority: 'medium' as const,
        assignee: 'user-002',
        assigneeName: '李技术员',
        dueDate: '2024-03-20T00:00:00Z',
        createdAt: '2024-01-12T10:00:00Z',
        updatedAt: '2024-01-12T10:00:00Z',
        projectId
      },
      {
        _id: 'task-003',
        title: '硬件电路设计',
        description: '设计电机驱动硬件电路，包括电源和接口电路',
        status: 'completed' as const,
        priority: 'high' as const,
        assignee: 'user-003',
        assigneeName: '王硬件工程师',
        dueDate: '2024-02-28T00:00:00Z',
        createdAt: '2024-01-08T08:00:00Z',
        updatedAt: '2024-02-25T16:00:00Z',
        projectId
      },
      {
        _id: 'task-004',
        title: '软件测试',
        description: '对电机控制软件进行全面测试，确保功能正常',
        status: 'pending' as const,
        priority: 'medium' as const,
        assignee: '',
        assigneeName: '',
        dueDate: '2024-03-25T00:00:00Z',
        createdAt: '2024-01-15T11:00:00Z',
        updatedAt: '2024-01-15T11:00:00Z',
        projectId
      },
      {
        _id: 'task-005',
        title: '客户需求分析',
        description: '分析客户的具体需求，制定详细的技术规格',
        status: 'cancelled' as const,
        priority: 'low' as const,
        assignee: 'user-004',
        assigneeName: '赵产品经理',
        dueDate: '2024-01-20T00:00:00Z',
        createdAt: '2024-01-05T13:00:00Z',
        updatedAt: '2024-01-18T15:00:00Z',
        projectId
      }
    ]

    return NextResponse.json(tasks)
  } catch (error) {
    console.error('获取项目任务失败:', error)
    return NextResponse.json(
      { error: '获取项目任务失败' },
      { status: 500 }
    )
  }
}

export async function POST(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const projectId = (await params).id
    const body = await request.json()
    
    // 这里应该创建新任务的逻辑
    console.log('创建新任务:', { projectId, ...body })
    
    return NextResponse.json({ 
      message: '任务创建成功',
      task: {
        _id: `task-${Date.now()}`,
        title: body.title,
        description: body.description,
        status: 'pending',
        priority: body.priority || 'medium',
        assignee: body.assignee || '',
        assigneeName: body.assigneeName || '',
        dueDate: body.dueDate,
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString(),
        projectId
      }
    })
  } catch (error) {
    console.error('创建项目任务失败:', error)
    return NextResponse.json(
      { error: '创建项目任务失败' },
      { status: 500 }
    )
  }
}